package com.blink.optimize.apis.string;

import org.junit.jupiter.api.Test;

/**
 * <Description> </Description>
 * <ClassName> TestCompareStringBuilderAndStringBuffer</ClassName>
 *
 * @Author liuxianzhao
 * @Date 2018年01月08日 16:06
 */
public class TestCompareStringBuilderAndStringBuffer {
    StringBuilder builder = new StringBuilder();
    StringBuffer buffer = new StringBuffer();

    private void append(String message) {
        for (int i = 0; i < 10; i++) {
            builder.append(i);
            buffer.append(i);
        }
        buffer.append("\n");
    }

    private void printBuilder() {
        System.out.println("打印builder开始");
        System.out.println(builder.toString());
        System.out.println("打印builder结束");
    }

    private void printBuffer() {
        System.out.println("打印buffer开始");
        System.out.println(buffer.toString());
        System.out.println("打印buffer结束");
    }

    @Test
    public void testMultiThread() {
        TestCompareStringBuilderAndStringBuffer testCompareStringBuilderAndStringBuffer = new TestCompareStringBuilderAndStringBuffer();
        Task task1 = new Task("task1", testCompareStringBuilderAndStringBuffer);
        Task task2 = new Task("task2", testCompareStringBuilderAndStringBuffer);
        Task task3 = new Task("task3", testCompareStringBuilderAndStringBuffer);
        task1.start();
        task2.start();
        task3.start();
        try {
            task1.join();
            task2.join();
            task3.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    class Task extends Thread {
        private String message;
        private TestCompareStringBuilderAndStringBuffer testCompareStringBuilderAndStringBuffer;

        public Task(String message, TestCompareStringBuilderAndStringBuffer testCompareStringBuilderAndStringBuffer) {
            this.message = message;
            this.testCompareStringBuilderAndStringBuffer = testCompareStringBuilderAndStringBuffer;
        }

        @Override
        public void run() {
            testCompareStringBuilderAndStringBuffer.append(message);
            testCompareStringBuilderAndStringBuffer.printBuffer();
            testCompareStringBuilderAndStringBuffer.printBuilder();
            try {
                sleep(10000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
